/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test.io;

import java.io.InputStream;

import org.junit.Test;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.LyciaReaderType;
import org.sourceprojects.lycia.annotations.Attribute;
import org.sourceprojects.lycia.annotations.Child;
import org.sourceprojects.lycia.annotations.ContextObject;
import org.sourceprojects.lycia.annotations.Tag;
import org.sourceprojects.lycia.exceptions.LyciaParsingException;
import org.sourceprojects.lycia.fluent.FluentBuilder;
import org.w3c.dom.Element;

import static org.junit.Assert.*;
import static org.sourceprojects.lycia.fluent.FluentBuilder.*;

public class ExternalReaderTestCase {

	@Test
	public void testExternalReader() throws Exception {
		StringBuilder sb = new StringBuilder();

		FluentBuilder<StringBuilder> builder = prepare();
		LyciaParser<StringBuilder> parser = builder.configure(
				reader(LyciaReaderType.VisualAgeRPG)).parser(
				tag("FVDESCanvas", method("foundCanvas", this, String.class,
						String.class, Element.class, StringBuilder.class)))
				.configure(contextObject(sb)).build();

		InputStream is = getClass().getClassLoader().getResourceAsStream(
				"VisualAgeRPG.txt");

		parser.parse(is);

		assertEquals("CAN01CAN0101CAN0102", sb.toString());
	}

	@Test(expected = LyciaParsingException.class)
	public void testExternalReaderFail() throws Exception {
		StringBuilder sb = new StringBuilder();

		FluentBuilder<StringBuilder> builder = prepare();
		LyciaParser<StringBuilder> parser = builder.configure(
				reader(LyciaReaderType.VisualAgeRPG)).parser(
				tag("FVDESCanvas", method("foundCanvasFail", this,
						String.class, String.class, Element.class,
						StringBuilder.class))).configure(contextObject(sb))
				.build();

		InputStream is = getClass().getClassLoader().getResourceAsStream(
				"VisualAgeRPG.txt");

		parser.parse(is);

		assertEquals("CAN01CAN0101CAN0102", sb.toString());
	}

	@SuppressWarnings("unused")
	private void foundCanvas(String localName,
			@Attribute("MinorVersion") String minorVersion,
			@Child(value = @Tag("PartName"), maxLayers = 2) Element partName,
			@ContextObject StringBuilder sb) {

		sb.append(partName.getTextContent());
	}

	@SuppressWarnings("unused")
	private void foundCanvasFail(String localName,
			@Attribute("MinorVersion") String minorVersion,
			@Child(value = @Tag("PartName")) Element partName,
			@ContextObject StringBuilder sb) {

		sb.append(partName.getTextContent());
	}

}
